//
//  ZOEEmtpyView.m
//  HealthyCity
//
//  Created by xxb on 2017/12/13.
//  Copyright © 2017年 智业互联网络科技有限公司 艾嘉健康. All rights reserved.
//

#import "ZOEEmtpyView.h"

@interface ZOEEmtpyView()

@property (nonatomic, strong) UIImageView *emptyIV;
@property (nonatomic, strong) UILabel *emptyLabel;
@property (nonatomic, strong) UIButton *button;

@property (nonatomic, assign) BOOL showEmptyLabel;
@property (nonatomic, assign) BOOL showEmptyIV;
@property (nonatomic, assign) BOOL showButton;

/**
 图片（文字）与父view的间距
 */
@property (nonatomic, assign) CGFloat offset;

@end

@implementation ZOEEmtpyView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.fontSize = 16;
        self.normalTextColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        self.responseTextColor = [UIColor colorWithRed:31/255.0 green:171/255.0 blue:255/255.0 alpha:1];
        self.intervalImageText = 25;
        self.linespace = 10;
    }
    return self;
}

#pragma mark - Event

- (void)showEmptyInView:(UIView*)view withNormalTip:(NSString*)normalTip responseTip:(NSString*)responseTip image:(NSString*)imageName offset:(CGFloat)offset {
    [self removeEmptyView];
    
    if(CGSizeEqualToSize(self.parentViewSize, CGSizeZero)){
        self.parentViewSize = view.bounds.size;
    }
    //赋值
    _offset = offset;
    if([[normalTip stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0 ||
       [[responseTip stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0){
        NSString *text;
        if([[normalTip stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0 &&
           [[responseTip stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0){
            text = [NSString stringWithFormat:@"%@%@", normalTip, responseTip];
        }else if([[responseTip stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0){
            text = [NSString stringWithFormat:@"%@", responseTip];
        }else{
            text = [NSString stringWithFormat:@"%@", normalTip];
        }
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.maximumLineHeight = _emptyLabel.font.lineHeight;//对汉字来说，应该就是暂居ascender这个高度
        CGSize size = [text boundingRectWithSize:CGSizeMake(self.parentViewSize.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.emptyLabel.font} context:nil].size;
        if(size.height > self.emptyLabel.font.lineHeight){
            paragraphStyle.lineSpacing = self.linespace;
        }
        paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
        [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attrStr.string.length)];
        [attrStr addAttribute:NSForegroundColorAttributeName value:self.responseTextColor range:NSMakeRange(normalTip.length, responseTip.length)];
        self.showEmptyLabel = YES;
        self.emptyLabel.attributedText = attrStr;
        if([[responseTip stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0){
            self.showButton = YES;
            [self button];
        }
    }
    if([[imageName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0){
        self.showEmptyIV = YES;
        self.emptyIV.image = [UIImage imageNamed:imageName];
    }
    //更新布局
    if(self.direction == ImageTextDirectionHorizontal){
        [self updateFrameHorizontal];
    }else{
        _emptyLabel.textAlignment = NSTextAlignmentCenter;
        [self updateFrameVertical];
    }
    //展示
    if(self.showEmptyIV){
        [view addSubview:_emptyIV];
    }
    if(self.showEmptyLabel){
        [view addSubview:_emptyLabel];
    }
    if(self.showButton){
        [view addSubview:_button];
    }
}

- (void)showEmptyInView:(UIView*)view withNormalTip:(NSString*)normalTip image:(NSString*)imageName offset:(CGFloat)offset {
    [self showEmptyInView:view withNormalTip:normalTip responseTip:nil image:imageName offset:offset];
}

- (void)showEmptyInView:(UIView*)view withImage:(NSString*)imageName offset:(CGFloat)offset {
    [self showEmptyInView:view withNormalTip:nil responseTip:nil image:imageName offset:offset];
}

- (void)showEmptyInView:(UIView*)view withNormalTip:(NSString*)normalTip responseTip:(NSString*)responseTip offset:(CGFloat)offset {
    [self showEmptyInView:view withNormalTip:normalTip responseTip:responseTip image:nil offset:offset];
}

- (void)showEmptyInView:(UIView*)view withNormalTip:(NSString*)normalTip offset:(CGFloat)offset {
    [self showEmptyInView:view withNormalTip:normalTip responseTip:nil image:nil offset:offset];
}

- (void)removeEmptyView {
    [_emptyIV removeFromSuperview];
    [_emptyLabel removeFromSuperview];
    [_button removeFromSuperview];
    //以防同个页面会有不同的空白提示
    self.showEmptyIV = NO;
    self.showEmptyLabel = NO;
    self.showButton = NO;
}

- (void)updateFrameVertical {
    CGFloat y = self.offset;
    CGFloat by = y;
    CGFloat bx = 0;
    CGFloat bw = 0;
    CGFloat bh = 0;
    if(self.showEmptyIV){
        CGRect ivRect = _emptyIV.frame;
        ivRect.size = CGSizeMake(self.emptyIV.image.size.width, self.emptyIV.image.size.height);
        ivRect.origin = CGPointMake((self.parentViewSize.width - ivRect.size.width) / 2, y);
        self.emptyIV.frame = ivRect;
        y += ivRect.size.height + self.intervalImageText;
        bx = ivRect.origin.x;
        bw = ivRect.size.width;
        bh = ivRect.size.height;
    }
    if(self.showEmptyLabel){
        CGSize size = [_emptyLabel sizeThatFits:CGSizeMake(self.parentViewSize.width, 0)];
        CGRect rect = _emptyLabel.frame;
        rect.size = size;
        rect.origin = CGPointMake((self.parentViewSize.width - size.width) / 2, y);
        _emptyLabel.frame = rect;
        bx = (bx != 0 && bx < rect.origin.x) ? bx : rect.origin.x;
        bw = bw > size.width ? bw : size.width;
        bh = bh == 0 ? rect.size.height : (bh + rect.size.height + self.intervalImageText);
    }
    if(self.showButton){
        _button.frame = CGRectMake(bx, by, bw, bh);
    }
}

- (void)updateFrameHorizontal {
    CGFloat x = self.offset;
    CGFloat bx = x;
    CGFloat by = 0;
    CGFloat bw = 0;
    CGFloat bh = 0;
    if(self.showEmptyIV){
        CGRect ivRect = _emptyIV.frame;
        ivRect.size = CGSizeMake(self.emptyIV.image.size.width, self.emptyIV.image.size.height);
        ivRect.origin = CGPointMake(x, (self.parentViewSize.height - ivRect.size.height) / 2);
        self.emptyIV.frame = ivRect;
        x += ivRect.size.width + self.intervalImageText;
        by = ivRect.origin.y;
        bw = ivRect.size.width;
        bh = ivRect.size.height;
    }
    if(self.showEmptyLabel){
        CGSize size = [_emptyLabel sizeThatFits:CGSizeMake(self.parentViewSize.width - x, 0)];
        CGRect rect = _emptyLabel.frame;
        rect.size = size;
        rect.origin = CGPointMake(x, (self.parentViewSize.height - size.height) / 2);
        _emptyLabel.frame = rect;
        by = (by != 0 && by < rect.origin.y) ? by : rect.origin.y;
        bw = bw == 0 ? size.width : (bw + size.width + self.intervalImageText);
        bh = bh > size.height ? bh : size.height;
    }
    if(self.showButton){
        _button.frame = CGRectMake(bx, by, bw, bh);
    }
}

#pragma mark - Action

- (void)click {
    if([self.delegate respondsToSelector:@selector(emtpyViewClick)]){
        [self.delegate emtpyViewClick];
    }
}

#pragma mark - Property

- (UIImageView *)emptyIV {
    if(!_emptyIV){
        _emptyIV = [[UIImageView alloc] init];
    }
    return _emptyIV;
}

- (UILabel*)emptyLabel {
    if(!_emptyLabel){
        _emptyLabel = [[UILabel alloc] init];
        _emptyLabel.font = [UIFont systemFontOfSize:self.fontSize];
        _emptyLabel.textColor = self.normalTextColor;
        _emptyLabel.numberOfLines = 0;
    }
    return _emptyLabel;
}

- (UIButton*)button {
    if(!_button){
        _button = [[UIButton alloc] init];
        [_button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

@end

